package cn.com.haopy.yue.ai.wx.config;


import cn.com.haopy.yue.ai.wx.constant.Constant;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger在线文档配置<br>
 * 项目启动后可通过地址：http://host:ip/swagger-ui.html 查看在线文档
 *
 * @author enilu
 * @version 2018-07-24
 */

@Configuration
@EnableSwagger2
@EnableKnife4j
@ConditionalOnProperty(name = "swagger.production", havingValue = "false")
public class WxRiskSwagger2Configuration {
    @Bean
    public Docket riskWxDocket() {
        //=====添加head参数start============================
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name(Constant.WX_LOGIN_TOKEN_KEY).description("AccessToken令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        // =========添加head参数end===================

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                //.host("https://www.baidu.com")
                .apiInfo(riskWxApiInfo())
                .groupName("wxRiskApi")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.com.haopy.yue.ai.wx.web"))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .enableUrlTemplating(true)
                //.extensions(openApiExtensionResolver.buildExtensions(groupName))
                //.extensions(openApiExtensionResolver.buildSettingExtensions())
                .globalOperationParameters(pars)
                //.securityContexts(securityContexts).securitySchemes(securitySchemes);
                //.securityContexts(CollectionUtil.newArrayList(securityContext()))
                //.securitySchemes(CollectionUtil.newArrayList(apiKey()))
                ;
        return docket;
    }

    private ApiInfo riskWxApiInfo() {
        return new ApiInfoBuilder()
                .title("Haopy_risk_API")
                .description("Haopy_risk_API")
                .termsOfServiceUrl("https://gitee.com/haopy/wxApi")
                .version("1.0")
                .build();
    }
}
